﻿using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace PNProject
{
    public class TextMediaTypeFormatter : MediaTypeFormatter
    {
        public TextMediaTypeFormatter()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/json"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/javascript"));
        }

        //public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, System.Net.Http.Headers.HttpContentHeaders content, IFormatterLogger formatterLogger)
        //{
        //    var taskCompletionSource = new TaskCompletionSource<object>();
        //    try
        //    {
        //        var memoryStream = new MemoryStream();
        //        readStream.CopyTo(memoryStream);
        //        var s = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
        //        taskCompletionSource.SetResult(s);
        //    }
        //    catch (Exception e)
        //    {
        //        taskCompletionSource.SetException(e);
        //    }

        //    return taskCompletionSource.Task;
        //}

        public override bool CanReadType(Type type)
        {
            return type == typeof(string);
        }

        public override bool CanWriteType(Type type)
        {
            return false;
        }
    }
}